﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using FluentAssertions;
using MementoClient.Interfaces;
using MementoClient.ViewModels;
using MementoClientConnectorInterfaces;
using MementoLibrary.DTOs;
using MementoWeb.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using MementoClient.Classes;

namespace MementoClient.Tests.Unit
{
    [TestClass]
    public class MainViewModelTest
    {
        [TestInitialize]
        public void Setup()
        {
            ClientAutoMapperConfiguration.RegisterAutoMapperMaps();
        }

        //[TestMethod]
        //public void ProjectNames_ReturnsProjectNamesFromTasks()
        //{
        //    var tasks = new List<Task>();
        //    tasks.Add(new Task { ProjectName = "P1", TaskName = "T1" });
        //    tasks.Add(new Task { ProjectName = "P2", TaskName = "T2" });
        //    var clientDataContainer = new ClientDataContainer
        //                                  {
        //                                      Tasks = tasks
        //                                  };
        //    Mock<IDataRepository> dataRepositoryMock = TestHelpers.GetClientDataRepositoryMock(clientDataContainer);

        //    var mainViewModel = new MainViewModel("", dataRepositoryMock.Object); 
        //    mainViewModel.ProjectNames.Should().BeEquivalentTo(new[] { "P1", "P2" });
        //}

        //[TestMethod]
        //public void ProjectNames_ReturnsTasksNamesWhenProjectSelected()
        //{
        //    var tasks = new List<Task>();
        //    tasks.Add(new Task { ProjectName = "P1", TaskName = "T1" });
        //    tasks.Add(new Task { ProjectName = "P2", TaskName = "T2" });
        //    var clientDataContainer = new ClientDataContainer
        //    {
        //        Tasks = tasks
        //    };

        //    Mock<IDataRepository> dataRepositoryMock = TestHelpers.GetClientDataRepositoryMock(clientDataContainer);

        //    var mainViewModel = new MainViewModel("", dataRepositoryMock.Object);
        //    mainViewModel.SelectedProjectName = "P2";
        //    mainViewModel.TasksInSelectedProject.First().TaskName.Should().Be("T2");
        //}

        [TestMethod]
        public void IfMainViewModelHasWorkLogSet_HasActiveWorklog_ReturnsTrue()
        {
            IDataRepository dataRepository = TestHelpers.GetClientDataRepositoryMock(new ClientDataContainer()).Object;
            var mainViewModel = new MainViewModel(dataRepository);
            mainViewModel.WorkLog = new WorkLogVM();
            mainViewModel.HasActiveWorklog.Should().BeTrue();
        }

        [TestMethod]
        public void IfMainViewModelHasNoWorkLogSet_HasActiveWorklog_ReturnsFalse()
        {
            IDataRepository dataRepository = TestHelpers.GetClientDataRepositoryMock(new ClientDataContainer()).Object;
            var mainViewModel = new MainViewModel(dataRepository);
            mainViewModel.HasActiveWorklog.Should().BeFalse();
        }

        [TestMethod]
        public void IfMainViewModelHasSelectedTaskSet_HasSelectedTask_ReturnsTrue()
        {
            IDataRepository dataRepository = TestHelpers.GetClientDataRepositoryMock(new ClientDataContainer()).Object;
            var mainViewModel = new MainViewModel(dataRepository);
            mainViewModel.SelectedTask = new Task();
            //mainViewModel.HasSelectedTask.Should().BeTrue();
        }

        [TestMethod]
        public void IfMainViewModelHasNoSelectedTaskSet_HasSelectedTask_ReturnsFalse()
        {
            IDataRepository dataRepository = TestHelpers.GetClientDataRepositoryMock(new ClientDataContainer()).Object;
            var mainViewModel = new MainViewModel(dataRepository);
            //mainViewModel.HasSelectedTask.Should().BeFalse();
        }

        [TestMethod]
        public void IfMainViewModelHasNoSelectedTaskSet_SelectedTaskHasWorklogs_ReturnsFalse()
        {
            IDataRepository dataRepository = TestHelpers.GetClientDataRepositoryMock(new ClientDataContainer()).Object;
            var mainViewModel = new MainViewModel(dataRepository);
            //mainViewModel.SelectedTaskHasWorklogs.Should().BeFalse();
        }

        [TestMethod]
        public void IfMainViewModelHasSelectedTaskSet_ButThereAreNoWorklogsMatchIt_SelectedTaskHasWorklogs_ReturnsFalse()
        {
            IDataRepository dataRepository = TestHelpers.GetClientDataRepositoryMock(new ClientDataContainer()).Object;
            var mainViewModel = new MainViewModel(dataRepository);
            mainViewModel.SelectedTask = new Task() {TaskID = "TestTaskID"};
            //mainViewModel.SelectedTaskHasWorklogs.Should().BeFalse();
        }

        [TestMethod]
        public void IfMainViewModelHasSelectedTaskSet_ButNoWorklogsMatchIt_SelectedTaskHasWorklogs_ReturnsFalse()
        {
            var clientDataContainer = new ClientDataContainer();
            clientDataContainer.Tasks = new [] { new Task { ProjectID = "", ProjectName = "", TaskName = "", TaskID = "AnotherTaskTestID"}};
            clientDataContainer.WorkLogs = new[] {new WorkLogDTO {Task = new TaskDTO {SourceSystem = "AnotherTaskTestID"}}};
            IDataRepository dataRepository = TestHelpers.GetClientDataRepositoryMock(clientDataContainer).Object;
            var mainViewModel = new MainViewModel(dataRepository);
            mainViewModel.SelectedTask = new Task() { TaskID = "TestTaskID" };
            //mainViewModel.SelectedTaskHasWorklogs.Should().BeFalse();
        }

        [TestMethod]
        public void IfMainViewModelHasSelectedTaskSet_AndWorklogsMatchIt_SelectedTaskHasWorklogs_ReturnsTrue()
        {
            var clientDataContainer = new ClientDataContainer();
            clientDataContainer.Tasks = new[] { new Task { ProjectID = "", ProjectName = "", TaskName = "", TaskID = "AnotherTaskTestID" } };
            clientDataContainer.WorkLogs = new[] { new WorkLogDTO { Task = new TaskDTO { SourceSystem = "AnotherTaskTestID" } } };
            IDataRepository dataRepository = TestHelpers.GetClientDataRepositoryMock(clientDataContainer).Object;
            var mainViewModel = new MainViewModel(dataRepository);
            mainViewModel.SelectedTask = clientDataContainer.Tasks.First();
            //mainViewModel.SelectedTaskHasWorklogs.Should().BeTrue();
        }

        [TestMethod]
        public void WhenEditThisWorklogCalled_WorkLogDoesNotRecieveSameInstanceAsSelectedWorkLog()
        {
            var clientDataContainer = new ClientDataContainer();
            clientDataContainer.Tasks = new[] { new Task { ProjectID = "", ProjectName = "", TaskName = "", TaskID = "AnotherTaskTestID" } };
            clientDataContainer.WorkLogs = new[] { new WorkLogDTO { Task = new TaskDTO { SourceSystem = "AnotherTaskTestID" } } };
            IDataRepository dataRepository = TestHelpers.GetClientDataRepositoryMock(clientDataContainer).Object;
            var mainViewModel = new MainViewModel(dataRepository);
            mainViewModel.SelectedWorklog = new WorkLogVM() {};
            mainViewModel.EditThisWorklogExecute(null);
            mainViewModel.WorkLog.Should().NotBeSameAs(mainViewModel.SelectedWorklog);
        }
    }
}
